package ua.edu.sumdu.j2se.roman.lab1.domain;

import java.util.*;
/**
 * Author: Radko Roman.
 */

/**
 * This class describes two methods that operates with Collection of tasks.
 */
public class Tasks {
    /**
     * This method returns array of the tasks which have start time that contained in time interval
     * from the initial time point that equals "from" parameter to final time point that equals "to" parameter.
     *
     * @param from  initial time point
     * @param to    final time point
     * @return      array of result elements
     */
    public static Set<Task> incoming(Iterable<Task> tasks, Date from, Date to) {
        Set<Task> result = new HashSet<Task>();
        for (Task task : tasks) {
            if (task.isInInterval(from, to)) {
                result.add(task);
            }
        }
        return result;
    }

    /**
     * This method add to the SortedMap all sets of tasks with dates which contains in borders: start < date <= end
     *
     * @param tasks collection of tasks
     * @param start Date
     * @param end   Date
     * @return      TreeMap with key Date type and HashSet<tasks> value
     */
    public static SortedMap<Date, Set<Task>> timeline(Iterable<Task> tasks, Date start, Date end) {
        SortedMap<Date, Set<Task>> map = new TreeMap<Date, Set<Task>>();
        Set<Task> t = incoming(tasks, start, end);
        SortedSet<Date> dateKeys = new TreeSet<Date>();
        // Creates set of all keys
        for (Task task : t) {
            dateKeys.addAll(task.getDatesInTask(start, end));
        }
        for (Date date : dateKeys) {
            Set<Task> taskSet = new HashSet<Task>();
            for (Task task : t) {
                if (task.getDatesInTask(start, end).contains(date))
                    taskSet.add(task);
            }
            map.put(date, taskSet);
        }
        return map;
    }
}






